let target = [];
let matrix = [];
let visited = [];
let n = 0;

function dfs(index) {
  visited[index] = 1;
  let i = 0;
  while (i < n) {
    if (matrix[index][i] == 1 && visited[i] == 0) {
      dfs(i);
    }
    i += 1;
  }
}

function main(n_input, target_input, matrix_input) {
  target = target_input
  n = n_input
  matrix = matrix_input
  visited = new Array(n);
  for (let i = 0; i < n; i++) {
    visited[i] = 0;
  }

  for (let i = 0; i < target.length; i++) {
    dfs(target[i]);
  }

  let result = 0;
  let i = 0;
  while (true) {
    if (i >= n) {
      break;
    } else {
      if (visited[i] == 1) {
        let flag = false;
        for (let j = 0; j < target.length; j++) {
          if (target[j] == i) {
            flag = true;
            break;
          }
        }
        if (!flag) {
          result += 1;
        }
      }
    }
    i += 1;
  }
  console.log(result)
}

main(5,
  [1, 2],
  [[1, 1, 0, 1, 0],
  [1, 1, 0, 0, 0],
  [0, 0, 1, 0, 1],
  [1, 0, 0, 1, 0],
  [0, 0, 1, 0, 1]])